package com.java.function;

import com.java.function.bean.Transaction;

import java.util.*;
import java.util.stream.Collectors;

public class App {

    /**
     *
     * 静态方法引用
     * 引用对象的实例方法
     * 引用某个对象的任意对象的实例方法
     * 引用构造函数
     *
     */


    /**
     * 按照交易金额进行刷选大于1000的记录，并按币种进行分类
     * map 的key为币种 值为price大于1000的对象集合
     *
     * @param transactions
     * @return
     */
    private static Map<String, List<Transaction>> filter1(List<Transaction> transactions) {
        Map<String, List<Transaction>> resultMap = new HashMap<>();
        for (Transaction transaction : transactions) {
            if (transaction.getPrice() > 1000) {
                String currency = transaction.getCurrency();
                List<Transaction> transactionList = resultMap.get(currency);
                if (transactionList == null) {
                    transactionList = new ArrayList<>();
                    resultMap.put(currency, transactionList);
                }
                transactionList.add(transaction);
            }
        }
        return resultMap;
    }

    /**
     * 按照交易金额进行刷选大于1000的记录，并按币种进行分类
     * map 的key为币种 值为price大于1000的对象集合
     *
     * @param transactions
     * @return
     */
    private static Map<String, List<Transaction>> filter2(List<Transaction> transactions) {


        //Xxx:getXxx 方法引用

        /**
         *
         * 静态方法引用
         * 引用对象的实例方法
         * 引用某个对象的任意对象的实例方法
         * 引用构造函数
         *
         */

        //并行流将stream改为parallelStream

        return transactions.stream().filter(transaction -> transaction.getPrice() > 1000)
                .collect(Collectors.groupingBy(Transaction::getCurrency));
    }

    public List<Integer> stream(List<Transaction> transactions) {
        //并行流将stream改为parallelStream
        return transactions.stream().filter(transaction -> transaction.getPrice() > 500).
                sorted(Comparator.comparing(Transaction::getPrice)).
                map(Transaction::getPrice).collect(Collectors.toList());

    }

    public static void main(String[] args) {
        List<Transaction> transactions = new ArrayList<>();
        transactions.add(new Transaction(1000, "人民币"));
        transactions.add(new Transaction(200, "美元"));
        transactions.add(new Transaction(3000, "欧元"));
        transactions.add(new Transaction(150, "日元"));
        transactions.add(new Transaction(2560, "人民币"));
        transactions.add(new Transaction(4562, "人民币"));
        transactions.add(new Transaction(25, "欧元"));
        transactions.add(new Transaction(589, "美元"));

        //常规编程方法输出
        System.out.println(App.filter1(transactions));

        //java8方法输出
        System.out.println(App.filter2(transactions));
    }
}
